<template>
  <page-header-wrapper>
    <page-tab-list></page-tab-list>
    <a-card :border="false">
      <p class="title">公众号设置</p>

      <a-alert style="margin-bottom:15px;" v-if="info" :message="'公众号后台服务器地址：' + info" type="info" />
      
      <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="{ span: 3 }" :wrapper-col="{ span: 8 }">
        <a-form-model-item label="公众号app_id" prop="app_id">
          <a-input v-model="form.app_id" placeholder="请输入公众号app_id" />
        </a-form-model-item>
        <a-form-model-item label="公众号token" prop="token">
          <a-input v-model="form.token" placeholder="请输入公众号token" />
        </a-form-model-item>
        <a-form-model-item label="公众号secret" prop="secret">
          <a-input v-model="form.secret" placeholder="请输入公众号secret" />
        </a-form-model-item>
        <a-form-model-item label="公众号aes_key" prop="aes_key" help="消息加解密密钥(EncodingAESKey)">
          <a-input v-model="form.aes_key" placeholder="请输入公众号aes_key" />
        </a-form-model-item>

        <a-form-model-item :wrapper-col="{ span: 14, offset: 4 }">
          <a-button type="primary" @click="submitForm" :loading="modalLoading"> 提交 </a-button>
          <a-button style="margin-left: 10px" @click="$router.back(-1)"> 取消 </a-button>
        </a-form-model-item>
      </a-form-model>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { wechatMpConfConfSave, wechatMpConfConf } from '@/api/wechartVipCard'

export default {
  data() {
    return {
      info: '',
      form: {
        app_id: '',
        token: '',
        aes_key: '',
        secret: ''
      },
      rules: {
        app_id: [{ required: true, message: '请输入公众号app_id', trigger: 'blur' }],
        token: [{ required: true, message: '请输入公众号token', trigger: 'blur' }],
        aes_key: [{ required: true, message: '请输入公众号aes_key', trigger: 'blur' }],
        secret: [{ required: true, message: '请输入公众号secret', trigger: 'blur' }]
      },
      modalLoading: false
    }
  },

  methods: {
    // 表单提交
    submitForm() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.modalLoading = true
          let { form } = this

          wechatMpConfConfSave({
            conf: form
          })
            .then(res => {
              this.modalLoading = false

              if (res.code !== 0) {
                this.$error({
                  content: res.msg
                })
              } else {
                this.$message.success(res.msg)
                this.visible = false
              }
            })
            .catch(err => {
              this.$message.error(err.message)
              this.modalLoading = false
            })
        }
      })
    }
  },

  mounted() {
    wechatMpConfConf().then(res => {
      if (res.code === 0) {
        let { app_id, token, aes_key, secret } = res.data

        this.info = res.callback
        this.form = {
          app_id,
          token,
          aes_key,
          secret
        }
      }
    })
  }
}
</script>

<style lang="less" scoped>
.title {
  color: #000;
  font-size: 16px;
  margin-bottom: 10px;
}
.tip {
  font-size: 14px;
  color: #8c8c8c;
  display: block;
}
</style>
